/*
 * @lc app=leetcode.cn id=304 lang=cpp
 *
 * [304] 二维区域和检索 - 矩阵不可变
 */

// @lc code=start
class NumMatrix {
public:

    int row, col;
    vector<vector<int>> s;

    NumMatrix(vector<vector<int>>& matrix) {
        row = matrix.size();
        if (row > 0) {
            col = matrix[0].size();
            s.resize(row + 1, vector<int>(col + 1, 0));

            for (int i = 1; i <= row; i ++) {
                for (int j = 1; j <= col; j ++) {
                    s[i][j] += s[i-1][j] + s[i][j-1] - s[i-1][j-1] + matrix[i-1][j-1];
                }
            }
        }
    }
    
    int sumRegion(int row1, int col1, int row2, int col2) {
        if (!row || !col) return 0;
        row1++, row2++, col1 ++, col2++;
        return s[row2][col2] - s[row1 - 1][col2] - s[row2][col1 - 1] + s[row1-1][col1-1];
    }
};

/**
 * Your NumMatrix object will be instantiated and called as such:
 * NumMatrix* obj = new NumMatrix(matrix);
 * int param_1 = obj->sumRegion(row1,col1,row2,col2);
 */
// @lc code=end

